﻿using PSI.Helper;
using PSI.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Text;

namespace PSI.Services {
    public class FIdService : PSIBaseService {
        /// <summary>
        /// 插入最近访问的功能编号
        /// </summary>
        /// <param name="fid">功能编号</param>
        public void InsertRecentFId(string fid) {
            var user = UserService.GetLoginUser();

            var sql = @"insert into psi_recent_fid (user_id, fid, dt) 
                        values(@userId, @fid, @dt)";
            var p = CreateDbParameters();
            p.AddWithValue("userId", user.Id);
            p.AddWithValue("fid", fid);
            p.AddWithValue("dt", DateTime.Now);

            Execute(sql, p);
        }

        /// <summary>
        /// 返回当前用户使用最多的十个功能
        /// </summary>
        /// <returns></returns>
        public List<FId> GetRecentFid() {
            var result = new List<FId>();

            var user = UserService.GetLoginUser();

            var sql = @"
                    select f.id, f.name
                    from (select top 10 fid as fid, count(fid) as c
                    from psi_recent_fid
                    where user_id = @userId
                    group by fid
                    order by c desc ) v, psi_fid f
                    where v.fid = f.id
                    ";
            var p = CreateDbParameters();
            p.AddWithValue("userId", user.Id);
            Query(sql, p, reader => {
                result.Add(new FId {
                    Id = reader.GetString(0),
                    Name = reader.GetString(1)
                });
            });

            return result;
        }

        /// <summary>
        /// 返回开头是几最大的返回数
        /// </summary>
        /// <param name="Numberstart"></param>
        /// <returns></returns>
        public int GetMaxNumber(string Numberstart) {

            var result = new List<FId>();

            var str = new StringBuilder("select top(1) * from psi_fid where id like '" + Numberstart + "%' order by id desc");
            var p = CreateDbParameters();

         //   p.AddWithValue("num", Numberstart);

            Query(str.ToString(), reader => {
                result.Add(new FId {
                    Id = reader.GetString(0),
                    Name = reader.GetString(1)
                });
            });

            if (result.Count == 0)
                return 0;
            else
                return int.Parse(result[0].Id)+1;           
        }

        /// <summary>
        /// 根据FID 收索
        /// </summary>
        /// <returns></returns>
        public List<FId> GetFidList(string num) {

            var result = new List<FId>();
            var str = "select id,name from psi_fid where name like '%" + num + "%'";

            Query(str.ToString(), reader => {
                result.Add(new FId {
                    Id = reader.GetString(0),
                    Name = reader.GetString(1)
                });
            });
            return result;
        }

        /// <summary>
        /// 得到初级菜单最大序号,Parentid 为"" 是查询首行菜单
        /// </summary>
        /// <returns></returns>
        public int GetMaxMenuOrderByParentId(string parentid) {

            int num = 0;

            var str = "select top 1 menu_id,show_order from psi_main_menu where parent_id=@parentid order by show_order desc";

            var p = CreateDbParameters();

            p.AddWithValue("parentid", parentid);

            Query(str.ToString(), p, reader => {
                num = (reader.GetInt32(1)) + 1;
            });

            return num;

        }

        /// <summary>
        /// 根据Fid 得到记录
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public FId GetSingbyFID(string fid) {

            var result=new FId();

            var str = new StringBuilder("select id,name,ref_id from psi_fid where id=@fid");

            var p = CreateDbParameters();

            p.AddWithValue("fid",fid);

            Query(str.ToString(),p,ready=>{
               
                result.Id=ready.GetString(0);
                result.Name=ready.GetString(1);
                result.RefId=ready.GetString(2);
            });

            return result;

        }
    }
}